/**
 * 
 */
package com.flute.framework.util.ftp;

import org.apache.commons.net.ftp.FTP;

import com.flute.framework.search.ProviderInfo;
import com.flute.tools.util.StringUtil;

/**
 * a domain about of ftp config
 * 
 * @author mawg
 * 
 */
public class FtpConfig {

	private String server;
	private int port;
	private String username;
	private String password;
	private String location;
	private String charset;
	private int filetype;

	public static final int DEFAULT_FTP_PORT = 21;
	public static final int DEFAULT_FTP_FILE_TYPE = FTP.BINARY_FILE_TYPE;
	public static final String ASCII_FILE_TYPE = "ascii";
	public static final String BINARY_FILE_TYPE = "binary";
	public static final String DEFAULT_FTP_CHARSET = "GBK";
	public static final String DEFAULT_USERNAME = "ftp";
	public static final String DEFAULT_PASSWORD = "";

	public FtpConfig() {
		port = DEFAULT_FTP_PORT;
		charset = DEFAULT_FTP_CHARSET;
		filetype = DEFAULT_FTP_FILE_TYPE;
		username = DEFAULT_USERNAME;
		password = DEFAULT_PASSWORD;
	}

	public FtpConfig(String server, String location) {
		this();
		this.server = server;
		this.location = location;
	}

	public FtpConfig(String server, int port, String username, String password,
			String location, String charset, int filetype) {
		this(server, location);
		this.port = port;
		this.username = username;
		this.password = password;
		this.charset = charset;
		this.filetype = filetype;
	}

	public String getServer() {
		return server;
	}

	public void setServer(String server) {
		this.server = server;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}

	public int getFiletype() {
		return filetype;
	}

	public void setFiletype(int filetype) {
		this.filetype = filetype;
	}

	public static int getFileType(String fileType) {
		return ASCII_FILE_TYPE.equalsIgnoreCase(fileType) ? FTP.ASCII_FILE_TYPE
				: BINARY_FILE_TYPE.equalsIgnoreCase(fileType) ? FTP.BINARY_FILE_TYPE
						: DEFAULT_FTP_FILE_TYPE;
	}

	public static FtpConfig parse(ProviderInfo info) {
		if (info == null) {
			throw new NullPointerException("info=null");
		}
		int filetype = getFileType(info.getFiletype());
		int port = info.getPort() > 0 ? info.getPort() : DEFAULT_FTP_PORT;
		String username = StringUtil.isNullOrEmpty(info.getUsername()) ? DEFAULT_USERNAME
				: info.getUsername();
		String password = info.getPassword() != null ? info.getPassword()
				: DEFAULT_PASSWORD;
		String charSet = StringUtil.isNullOrEmpty(info.getCharset()) ? DEFAULT_FTP_CHARSET
				: info.getCharset();
		return new FtpConfig(info.getServer(), port, username, password, info
				.getWorkspace(), charSet, filetype);
	}

	public static void main(String[] args) {

	}
}
